package com.love.cloud.auth.tokengranter;


import com.love.cloud.common.security.token.MyAuthenticationToken;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AccountStatusException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 自定义token授权器
 * @author hongdongqu
 */
public abstract  class AbstractCustomTokenGranter extends AbstractTokenGranter {
    private final AuthenticationManager authenticationManager;

    protected AbstractCustomTokenGranter(AuthenticationManager authenticationManager, AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, String grantType) {
        super(tokenServices, clientDetailsService, requestFactory, grantType);
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
        Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());
        AbstractAuthenticationToken authRequest = getAuthenticationToken(parameters);
        //获取到oauth2权限信息
        OAuth2Authentication authentication=this.getOAuth2Authentication(authRequest,client,tokenRequest);
        authentication.setAuthenticated(true);
        return authentication;
    }

    /**
     * 获取到自定义的token
     * @param parameters
     * @return
     */
    protected abstract MyAuthenticationToken getAuthenticationToken(Map<String, String> parameters);
    /**
     * 获取到user的权限信息
     * @param userAuth
     * @param client
     * @param tokenRequest
     * @return
     */
    private OAuth2Authentication getOAuth2Authentication (Authentication userAuth, ClientDetails client, TokenRequest tokenRequest){
        try {
            userAuth = this.authenticationManager.authenticate(userAuth);
        } catch (AccountStatusException var8) {
            throw new InvalidGrantException(var8.getMessage());
        } catch (BadCredentialsException var9) {
            throw new InvalidGrantException(var9.getMessage());
        }
        if (userAuth != null && userAuth.isAuthenticated()) {
            OAuth2Request storedOAuth2Request = this.getRequestFactory().createOAuth2Request(client, tokenRequest);
            return new OAuth2Authentication(storedOAuth2Request, userAuth);
        } else {
            throw new InvalidGrantException("Could not authenticate user: " + userAuth.getPrincipal());
        }
    }
}
